/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.payment.web.controller;

import com.egzosn.pay.web.support.HttpRequestNoticeParams;
import com.medusa.gruul.common.core.annotation.EscapeLogin;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.common.data.tenant.TenantContextHolder;
import com.medusa.gruul.common.log.annotation.SysLog;
import com.medusa.gruul.payment.api.enums.PayType;
import com.medusa.gruul.payment.api.model.pay.PayParam;
import com.medusa.gruul.payment.api.model.pay.OrderPayParam;
import com.medusa.gruul.payment.api.model.pay.PayResult;
import com.medusa.gruul.payment.common.enums.TradeStatus;
import com.medusa.gruul.payment.service.MultiPayService;
import com.medusa.gruul.payment.service.MultiPayVerifyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 张治保
 * date 2021/12/14
 */
@Slf4j
@RestController
@RequestMapping("/multi/pay")
@RequiredArgsConstructor
public class MultiPayController {

    private final MultiPayService multiPayService;
    private final MultiPayVerifyService multiPayVerifyService;

    @SysLog("调起订单支付页面")
    @GetMapping(value = "/page")
    public Result<Object> toPayPage(@Validated OrderPayParam params){
        return Result.ok(
            multiPayService.toPay(params)
        );
    }

    @SysLog("获取调起支付所需数据")
    @PostMapping("/page")
    public Result<PayResult> toPayPage(@RequestBody @Validated PayParam payParam){
        return Result.ok(
            multiPayService.toPay(payParam)
        );
    }

    @EscapeLogin
    @SysLog("支付/退款 notify回调地址")
    @RequestMapping(value = "/notify/{tenantId}/{detailsId}")
    public String payBack(HttpServletRequest request,
                          @PathVariable String detailsId,
                          @PathVariable String tenantId
    ) {
        TenantContextHolder.setTenantId(tenantId);
        String result = multiPayService.notify(detailsId, new HttpRequestNoticeParams(request));
        TenantContextHolder.clear();
        return result;
    }

    @EscapeLogin
    @SysLog("重定向到页面跳转地址")
    @RequestMapping(value = "/return/{tenantId}/{payType}")
    public void viewJump(
        @PathVariable PayType payType,
        @PathVariable String tenantId,
        @RequestParam("out_trade_no") String outTradeNo,
        HttpServletResponse response
    ) throws IOException {
        TenantContextHolder.setTenantId(tenantId);
        String jumpUrl = multiPayService.viewJump(payType, outTradeNo);
        TenantContextHolder.clear();
        response.sendRedirect( jumpUrl );
    }

    @SysLog("获取支付状态")
    @GetMapping("/trade/status/{payType}/{outTradeNo}")
    public Result<TradeStatus> tradeStatus(
        @PathVariable PayType payType,
        @PathVariable String outTradeNo
    ){
        return Result.ok(
            multiPayVerifyService.getPayStatus(payType, outTradeNo)
        );
    }
}
